package cn.javaSong.controller;

import cn.javaSong.common.ResponseModel;
import cn.javaSong.service.ISongService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dakai
 * @since 2024-10-26
 */
@RestController
@RequestMapping("/song")
public class SongController {

    @Resource
    private ISongService iSongService;

    @PostMapping("/uploadMp3")
    public ResponseModel uploadMp3File(@RequestParam("file") MultipartFile file) {
        return iSongService.addSong(file);
    }

    @PostMapping("/uploadFilesMp3")
    public ResponseModel uploadMp3File(@RequestParam("files") List<MultipartFile> files) {
        return iSongService.addSongs(files);
    }


    @GetMapping("/exportPlan")
    public void exportSongs(HttpServletResponse response) {
        try {
            iSongService.exportUsersToExcel(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/importPlan")
    public ResponseEntity<String> importSongs(@RequestParam("file") MultipartFile file) {
        try {
            iSongService.importUsersFromExcel(file);
            return ResponseEntity.ok("Songs imported successfully");
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error importing songs: " + e.getMessage());
        }
    }
}
